package com.genexususa.eo.bing.data.wsSubclasses;

import com.genexususa.eo.bing.CampaignManagementServiceClasses.*;
import com.genexususa.eo.bing.data.GxItemException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class GxSiteLinksAdExtension extends SiteLinksAdExtension {
    private long gxCampaignId;
    private long gxId;

    public GxSiteLinksAdExtension(Element element) throws IllegalArgumentException, GxItemException {

        // AdExtension2 parameters

        if (element.getElementsByTagName("GxCampaignId").getLength() > 0 &&
                element.getElementsByTagName("GxCampaignId").item(0).getChildNodes().getLength() > 0 &&
                Long.valueOf(((Node)element.getElementsByTagName("GxCampaignId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.gxCampaignId = Long.valueOf(((Node)element.getElementsByTagName("GxCampaignId").item(0).getChildNodes().item(0)).getNodeValue());
        } else {
            throw new GxItemException("Missing external Campaign id");
        }
        if (element.getElementsByTagName("GxSiteLinksAdExtensionId").getLength() > 0 &&
            element.getElementsByTagName("GxSiteLinksAdExtensionId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("GxSiteLinksAdExtensionId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.gxId = Long.valueOf(((Node)element.getElementsByTagName("GxSiteLinksAdExtensionId").item(0).getChildNodes().item(0)).getNodeValue());
        } else {
            throw new GxItemException("Missing external SiteLinksAdExtension id");
        }
        if (element.getElementsByTagName("BingSiteLinksAdExtensionId").getLength() > 0 &&
            element.getElementsByTagName("BingSiteLinksAdExtensionId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("BingSiteLinksAdExtensionId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.setId(Long.valueOf(((Node) element.getElementsByTagName("BingSiteLinksAdExtensionId").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("Status").getLength() > 0 &&
            element.getElementsByTagName("Status").item(0).getChildNodes().getLength() > 0) {
            if (((Node) element.getElementsByTagName("Status").item(0).getChildNodes().item(0)).getNodeValue().equals("A")) {
                this.setStatus(AdExtensionStatus.Active);
            } else if (((Node) element.getElementsByTagName("Status").item(0).getChildNodes().item(0)).getNodeValue().equals("D")) {
                this.setStatus(AdExtensionStatus.Deleted);
            }
        }
        if (element.getElementsByTagName("Type").getLength() > 0 &&
            element.getElementsByTagName("Type").item(0).getChildNodes().getLength() > 0) {
            this.setType(((Node) element.getElementsByTagName("Type").item(0).getChildNodes().item(0)).getNodeValue());
        }
        if (element.getElementsByTagName("Version").getLength() > 0 &&
            element.getElementsByTagName("Version").item(0).getChildNodes().getLength() > 0 &&
            Integer.valueOf(((Node)element.getElementsByTagName("Version").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.setVersion(Integer.valueOf(((Node) element.getElementsByTagName("Version").item(0).getChildNodes().item(0)).getNodeValue()));
        }

        // ProductAdExtension parameters

        NodeList siteLinksNode = element.getElementsByTagName("SiteLinkCollection");
        if (siteLinksNode.getLength() > 0) {
            NodeList productConditionCollectionNode = ((Element)siteLinksNode.item(0)).getElementsByTagName("SiteLink");
            SiteLink[] siteLinks = new SiteLink[siteLinksNode.getLength()];
            for (int temp = 0; temp < siteLinksNode.getLength(); temp++) {
                siteLinks[temp] = new GxSiteLink((Element)productConditionCollectionNode.item(temp));
            }
            this.setSiteLinks(siteLinks);
        }
    }

    public long getGxId() {
        return gxId;
    }

    public void setGxId(long gxId) {
        this.gxId = gxId;
    }

    public long getGxCampaignId() {
        return gxCampaignId;
    }

    public void setGxCampaignId(long gxCampaignId) {
        this.gxCampaignId = gxCampaignId;
    }
}
